package com.chzc.fm.duotin.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.TopicLinearLayoutAdapter;
import com.chzc.fm.duotin.bean.channel.Topic;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/6/25.
 */
public class TopicActivity extends AppCompatActivity implements View.OnClickListener{

    private RecyclerView recyclerView;

    private TopicLinearLayoutAdapter adapter;

    private ImageView ivback;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_topic);

        initViews();

        initDatas();

    }


    public void initViews()
    {
        recyclerView = (RecyclerView) findViewById(R.id.topic_conten_rl);
        ivback = (ImageView) findViewById(R.id.topic_back_iv);

        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        //设置RecycleView的item间距
        recyclerView.addItemDecoration(new SpacesItemDecoration(30));

        ivback.setOnClickListener(this);

    }

    public void initDatas()
    {
        getTopic(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    LogUtil.w("开始解析数据");

                    JSONObject root = new JSONObject(object.toString());

                    String error_msg = root.getString("error_msg");

                    if("SUCCESS".equals(error_msg))
                    {
                        JSONObject data = root.getJSONObject("data");

                        final JSONArray datalist = data.getJSONArray("data_list");

                        LogUtil.w("datalist>>>>>>>" + datalist);

                        adapter = new TopicLinearLayoutAdapter(getApplicationContext(),datalist);
                        recyclerView.setAdapter(adapter);

                        adapter.setItemOnClickListener(new TopicLinearLayoutAdapter.mItemOnClickListener() {
                            @Override
                            public void OnItemClick(int position) {
                                Intent intent;
                                try {
                                    JSONObject obj = datalist.getJSONObject(position);
                                    JSONObject object = obj.getJSONObject("topic");
                                    Topic topic = Topic.objectFromData(object.toString());
                                    if(topic.getAlbum_id() == 0) {
                                        intent = new Intent(TopicActivity.this, SpecificTopicActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable("topicitem",topic);
                                        intent.putExtras(bundle);
                                        startActivity(intent);
                                    }else
                                    {

                                    }



                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                    }



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }


    /**
     *  请求频道模块话题的数据
     * @param callback
     */
    public void getTopic(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return HttpUtil.doGet(GlobalParam.URL_TOPIC);
            }
        };

        new DuotinTask(request,callback).execute();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.topic_back_iv:
                finish();
                break;
        }
    }
}
